#include <iostream>
#include <fstream>
#include <string>
#include "contacts.pb.h"
using namespace std;

void PrintContacts(contacts2::Contacts &cs){
    for(int i = 0; i < cs.contacts_size(); i++){
        std::cout << "----------- 联系人 " << i + 1 << "-----------------" << endl;
        const contacts2::PeopleInfo &person = cs.contacts(i);
        cout << "姓名: " << person.name() << endl;
        cout << "年龄: " << person.age() << endl;
        for(int j = 0; j < person.phone_size(); j++){
            const contacts2::PeopleInfo::Phone &phone = person.phone(j);
            cout << "电话: " << j + 1 << " " << phone.number() <<  "  (" << phone.PhoneType_Name(phone.type()) << ")" << endl;
        }

        if (person.has_data() && person.data().Is < contacts2::Address>()){
            contacts2::Address addr;
            person.data().UnpackTo(&addr);
            if (!addr.home_address().empty()) cout << "家庭住址: " << addr.home_address() << endl;
            if (!addr.unit_address().empty()) cout << "单元住址: " << addr.unit_address() << endl;
        }

        switch (person.other_contact_case())
        {
        case contacts2::PeopleInfo::kQq:
            cout << "qq: " << person.qq() << std::endl;
            break;
        case contacts2::PeopleInfo::kWechat:
            std::cout << "wechat: " << person.wechat() << std::endl;
            break;
        default:
            break;
        }

        if (person.remark_size()){
            cout << "备注信息: " << endl;
            for (auto it = person.remark().cbegin(); it != person.remark().cend(); it++){
                cout << "     " << it->first << ": " << it->second << endl;
            }
        }
        cout << "------------------------" << endl;
    }
}

int main(){
    contacts2::Contacts contacts;
    // 读取本地人已存在的联系人文件
    fstream input("contacts.bin", ios::in | ios::binary);
    if(!contacts.ParseFromIstream(&input)){
        cout << "Failed to parse contacts.bin." << endl;
        input.close();
        return -1;
    }
    PrintContacts(contacts);
    input.close();
    
    return 0;
}